package com.sercomm.ott.mediaplayer;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRouter;
import android.net.ConnectivityManager;
import android.net.DhcpInfo;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.provider.Settings;
import android.text.Html;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Choreographer;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;
import android.view.Choreographer.FrameCallback;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import static android.R.id.list;

public class VideoViewActivity extends Activity implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener,
        MediaPlayer.OnPreparedListener, View.OnTouchListener ,Choreographer.FrameCallback{
    private final static String TAG = "VideoViewActivity";
    /**
     * View播放
     */
    private VideoView videoView;

    /**
     * 加载预览进度条
     */
    private ProgressBar progressBar;

    /**
     * 设置view播放控制条
     */
    private MediaController mediaController;

    /**
     * 标记当视频暂停时播放位置
     */
    private int intPositionWhenPause=-1;

    /**
     * 设置窗口模式下的videoview的宽度
     */
    private int videoWidth;

    /**
     * 设置窗口模式下videoview的高度
     */
    private int videoHeight;
    /*
     *hide control bar
     */
    //private View viewButtonId;
    //private View viewSeekbarTime;

    private String pathString;

    TextView vdec_status = null;
    private MyThread myThread = null;
    private boolean myThread_flag = false;
    private long videoFrameCount=0;

    private ArrayList<String> play_list = null;
    private int play_item = 0;
    private int play_tote =0;

    //HandlerThread test
    private HandlerThread mCheckMsgThread;
    private Handler mCheckMsgHandler;
    private boolean isUpdateInfo;
    private TextView updateInfo;
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.mplayer_videoview);
/*
        pathString = getIntent().getStringExtra("imFlowsUrlStr");
        if(pathString == null){
            pathString = "http://192.168.1.3/davis/1.mp4" ;
        }
        */
        //pathString = "udp://@227.1.1.1:1234" ;
        //pathString = "/sdcard/Movies/1.mp4";
        play_list = new ArrayList<>();
        play_list.add("/sdcard/Movies/1.mp4");
        play_list.add("/storage/1110-1F5A/davis/demo/Wildlife.wmv");
        play_list.add("/storage/1110-1F5A/davis/demo/adam_01_mpeg1_aac_1080p.mkv");
        pathString = play_list.get(play_item);
        play_tote = play_list.size();
        initVideoView();

//        Uri uri=Uri.parse("http://123.150.52.227/0725695b00000000-1415769042-1960016430/data5/vkplx.video.qq.com/flv/74/164/a0015193bxf.p203.1.mp4");
//        Intent intent=new Intent(Intent.ACTION_VIEW);
//        intent.setDataAndType(uri,"video/*");
//        startActivity(intent);
        SystemStatus.initSystemManager(this);
        myThread_flag = true;
        vdec_status = (TextView)findViewById(R.id.vdec_status);
        myThread = new MyThread();
        myThread.start();

        initHandlerTheard();
        updateInfo = (TextView)findViewById(R.id.update_info);
    }

    private void setVdecStatus(){
        List<String> tmp = SystemStatus.getVideoStatus();
        String vdec = SystemStatus.getStrVideoStatus();
        //Log.d(TAG, "-----davis----vdec_status " + tmp.trim());
        try {
            vdec_status.setText(tmp.get(0) +"\n"+vdec);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private final int MSG_VDEC_STATUS = 0x0001;
    private Handler myHandler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            switch (msg.what){
                case MSG_VDEC_STATUS:
                    setVdecStatus();
                    break;
            }
        }
    };
    public class MyThread extends Thread{
        @Override
        public void run() {
            while (myThread_flag) {
                try {
                    Thread.sleep(3000);
                    Message msg_vdec_status = new Message();
                    msg_vdec_status.what = MSG_VDEC_STATUS;
                    myHandler.sendMessage(msg_vdec_status);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     *  HandlerThread  test
     */
    private final static int MSG_UPDATE_INFO = 0x0003;
    private void initHandlerTheard(){
        mCheckMsgThread = new HandlerThread("check-msg-coming");
        mCheckMsgThread.start();
        mCheckMsgHandler = new Handler(mCheckMsgThread.getLooper()){
            @Override
            public void handleMessage(Message msg){
                checkUpdateInfo();
                if(isUpdateInfo){
                    mCheckMsgHandler.sendEmptyMessageDelayed(MSG_UPDATE_INFO,1000);
                }
            }
        };
    }
    private void checkUpdateInfo(){
        try{
            Thread.sleep(1000);
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    String result = "实时更新中，当前大盘指数：<font color='red'>%d</font>";
                    result = String.format(result, (int) (Math.random() * 3000 + 1000));
                    updateInfo.setText(Html.fromHtml(result));
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     *初始化videoview播放
     */
    public void initVideoView(){
        //初始化进度条
        progressBar= (ProgressBar) findViewById(R.id.progressBar);
        //初始化VideoView
        videoView= (VideoView) findViewById(R.id.videoView);
        //初始化videoview控制条
        mediaController=new MediaController(this);
        //设置videoview的控制条
        videoView.setMediaController(mediaController);
        //设置显示控制条
        mediaController.show(0);
        //设置播放完成以后监听
        videoView.setOnCompletionListener(this);
        //设置发生错误监听，如果不设置videoview会向用户提示发生错误
        videoView.setOnErrorListener(this);
        //设置在视频文件在加载完毕以后的回调函数
        videoView.setOnPreparedListener(this);
        //设置videoView的点击监听
        videoView.setOnTouchListener(this);
        //设置网络视频路径
        Uri uri=Uri.parse(pathString);
        videoView.setVideoURI(uri);
        //设置为全屏模式播放
        setVideoViewLayoutParams(1);
        //add postdelayed for support udp videostream
        videoView.postDelayed(new Runnable() {
            @Override
            public void run() {
                videoView.setVisibility(View.VISIBLE);
            }
        }, 500);

        videoView.setOnInfoListener(new MediaPlayer.OnInfoListener(){
            @Override
            public boolean onInfo(MediaPlayer mp,int what,int extra){
                switch (what){

                }
                return false;
            }

        });
    }
    @Override
    protected void onStart() {
        super.onStart();
        //启动视频播放
        videoView.start();
        //设置获取焦点
        videoView.setFocusable(true);
    }
    /**
     * 设置videiview的全屏和窗口模式
     * @param paramsType 标识 1为全屏模式 2为窗口模式
     */
    public void setVideoViewLayoutParams(int paramsType){

        if(1==paramsType) {
            RelativeLayout.LayoutParams LayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            videoView.setLayoutParams(LayoutParams);
        }else{
            //动态获取宽高
            DisplayMetrics DisplayMetrics=new DisplayMetrics();
            this.getWindowManager().getDefaultDisplay().getMetrics(DisplayMetrics);
            videoHeight=DisplayMetrics.heightPixels-50;
            videoWidth=DisplayMetrics.widthPixels-50;
            RelativeLayout.LayoutParams LayoutParams = new RelativeLayout.LayoutParams(videoWidth,videoHeight);
            LayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
            videoView.setLayoutParams(LayoutParams);
        }

    }
    /**
     * 视频播放完成以后调用的回调函数
     */
    @Override
    public void onCompletion(MediaPlayer mp) {
        play_item++;
        if(play_item <= play_tote-1) {
            pathString = play_list.get(play_item);
            initVideoView();
        }
    }
    /**
     * 视频播放发生错误时调用的回调函数
     */
    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        switch (what){
            case MediaPlayer.MEDIA_ERROR_UNKNOWN:
                Log.e("text","发生未知错误");

                break;
            case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
                Log.e("text","媒体服务器死机");
                break;
            default:
                Log.e("text","onError+"+what);
                break;
        }
        switch (extra){
            case MediaPlayer.MEDIA_ERROR_IO:
                //io读写错误
                Log.e("text","文件或网络相关的IO操作错误");
                break;
            case MediaPlayer.MEDIA_ERROR_MALFORMED:
                //文件格式不支持
                Log.e("text","比特流编码标准或文件不符合相关规范");
                break;
            case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
                //一些操作需要太长时间来完成,通常超过3 - 5秒。
                Log.e("text","操作超时");
                break;
            case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
                //比特流编码标准或文件符合相关规范,但媒体框架不支持该功能
                Log.e("text","比特流编码标准或文件符合相关规范,但媒体框架不支持该功能");
                break;
            default:
                Log.e("text","onError+"+extra);
                break;
        }
        //如果未指定回调函数， 或回调函数返回假，VideoView 会通知用户发生了错误。
        return false;
    }

    /**
     * 视频文件加载文成后调用的回调函数
     * @param mp
     */
    @Override
    public void onPrepared(MediaPlayer mp) {
     //如果文件加载成功,隐藏加载进度条
        progressBar.setVisibility(View.GONE);

    }

    /**
     * 对videoView的触摸监听
     * @param v
     * @param event
     * @return
     */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }



    /**
     * 页面暂停效果处理
     */
    @Override
    protected  void onPause() {
        super.onPause();
        //如果当前页面暂定则保存当前播放位置，并暂停
        intPositionWhenPause=videoView.getCurrentPosition();
        //停止回放视频文件
        videoView.stopPlayback();

        isUpdateInfo = false;
        mCheckMsgHandler.removeMessages(MSG_UPDATE_INFO);
    }

    /**
     * 页面从暂停中恢复
     */
    @Override
    protected void onResume() {
        super.onResume();
       //跳转到暂停时保存的位置
        if(intPositionWhenPause>=0){
            videoView.seekTo(intPositionWhenPause);
            //初始播放位置
            intPositionWhenPause=-1;
        }
        isUpdateInfo = true;
        mCheckMsgHandler.sendEmptyMessage(MSG_UPDATE_INFO);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(null!=videoView){
            videoView=null;
        }
        if(myThread_flag){
            myThread_flag = false;
        }
        mCheckMsgThread.quit();
    }

    @Override
    public void doFrame(long FrameTimeNanos){
        //videoFrameCount++;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        switch (keyCode){
            case KeyEvent.KEYCODE_DPAD_UP:
                if(play_item-1 >=0){
                    play_item--;
                    pathString = play_list.get(play_item);
                    initVideoView();
                }
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
                if(play_item+1 < play_tote){
                    play_item++;
                    pathString = play_list.get(play_item);
                    initVideoView();
                }
                break;
        }
        return super.onKeyDown(keyCode,event);
    }

}
